import axios from "axios";
import { useUserInfoStore } from '@/store/userInfo/index.js'

const http = axios.create({
    baseURL: 'http://117.72.76.156:9000',
    timeout: 8000,
})

http.interceptors.request.use(config => {

    const token = localStorage.getItem("token") || ''

    config.headers['token'] = token
    return config
}, error => {
    return Promise.reject(error)
});

// response 拦截器
// 可以在接口响应后统一处理结果
http.interceptors.response.use(
    response => {
        console.log('response', response)
        let res = response.data;
        console.log('res', res)
        if (res.code == 200) {
            return res.data
        }
        else if (res.code == 401) {
            useUserInfoStore().reset()
            localStorage.clear();
            ElMessage.error(res.msg || '操作失败')
            return false
        }
        else {
            console.log('=-==', res.msg)
            ElMessage.error(res.msg || '操作失败')
            return false;
        }
    },
    error => {
        console.log('err', error) // for debug
        return Promise.reject(error)
    }
)
export default http